package com.mystnihon.freeremote.db.table;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.mystnihon.freeremote.db.DBProvider;

public class ChannelTable extends GenericTable {

	public static final String TABLE_NAME = "channel";
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.freeremote." + TABLE_NAME;

	/**
	 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
	 */
	public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.freeremote." + TABLE_NAME;

	public static final Uri CONTENT_URI = Uri.parse("content://" + DBProvider.AUTHORITY + "/" + TABLE_NAME);

	public static final String COLUMN_CHANNEL_ID = "channel_id";
	public static final String COLUMN_CHANNEL_FREEBOX_ID = "channel_freebox_id";
	public static final String COLUMN_CHANNEL_NUMBER = "channel_number";
	public static final String COLUMN_CHANNEL_NAME = "channel_name";
	public static final String COLUMN_CHANNEL_IMAGE_NAME = "image_id";
	public static final String COLUMN_CHANNEL_LAST_UPDATE = "last_update";
	public static final String COLUMN_CHANNEL_IS_FAVORITE = "is_favorite";

	public int mChannelId;
	public int mFreeboxId;
	public int mChannelNumber;
	public String mName;
	public String mImage;
	public long mLastUpdate;
	public boolean mIsFavorite;

	// column position in projection
	public static final int CONTENT_CHANNEL_ID_COLUMN = 1;
	public static final int CONTENT_CHANNEL_FREEBOX_ID_COLUMN = 2;
	public static final int CONTENT_CHANNEL_NUMBER_COLUMN = 3;
	public static final int CONTENT_CHANNEL_NAME_COLUMN = 4;
	public static final int CONTENT_CHANNEL_IMAGE_NAME_COLUMN = 5;
	public static final int CONTENT_CHANNEL_LAST_UPDATE_COLUMN = 6;
	public static final int CONTENT_CHANNEL_IS_FAVORITE = 7;
	private static final String TAG = ChannelTable.class.getSimpleName();

	public static String[] CONTENT_PROJECTION = new String[] { _ID, COLUMN_CHANNEL_ID, COLUMN_CHANNEL_FREEBOX_ID, COLUMN_CHANNEL_NUMBER, COLUMN_CHANNEL_NAME, COLUMN_CHANNEL_IMAGE_NAME,
			COLUMN_CHANNEL_LAST_UPDATE, COLUMN_CHANNEL_IS_FAVORITE };

	public static String[] CONTENT_PROJECTION_SMALL = new String[] { _ID, COLUMN_CHANNEL_ID };

	public static String getCreateStatement() {
		final StringBuffer str = new StringBuffer("CREATE TABLE ");
		str.append(TABLE_NAME);
		str.append(" (");
		str.append(_ID);
		str.append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
		str.append(COLUMN_CHANNEL_ID);
		str.append(" INTEGER, ");
		str.append(COLUMN_CHANNEL_FREEBOX_ID);
		str.append(" INTEGER, ");
		str.append(COLUMN_CHANNEL_NUMBER);
		str.append(" INTEGER, ");
		str.append(COLUMN_CHANNEL_NAME);
		str.append(" TEXT, ");
		str.append(COLUMN_CHANNEL_IMAGE_NAME);
		str.append(" TEXT, ");
		str.append(COLUMN_CHANNEL_LAST_UPDATE);
		str.append(" INTEGER, ");
		str.append(COLUMN_CHANNEL_IS_FAVORITE);
		str.append(" INTEGER); ");

		Log.d(TAG, "create court table: " + str.toString());
		return str.toString();
	}

	public ContentValues getContentValues() {
		final ContentValues content = new ContentValues();
		content.put(COLUMN_CHANNEL_ID, mChannelId);
		content.put(COLUMN_CHANNEL_FREEBOX_ID, mFreeboxId);
		content.put(COLUMN_CHANNEL_NUMBER, mChannelNumber);
		content.put(COLUMN_CHANNEL_NAME, mName);
		content.put(COLUMN_CHANNEL_IMAGE_NAME, mImage);
		content.put(COLUMN_CHANNEL_LAST_UPDATE, mLastUpdate);
		content.put(COLUMN_CHANNEL_IS_FAVORITE, mIsFavorite);
		return content;
	}

	public static ChannelTable inflateObject(Cursor c) {
		final ChannelTable channel = new ChannelTable();
		channel.mChannelId = c.getInt(CONTENT_CHANNEL_ID_COLUMN);
		channel.mFreeboxId = c.getInt(CONTENT_CHANNEL_FREEBOX_ID_COLUMN);
		channel.mChannelNumber = c.getInt(CONTENT_CHANNEL_NUMBER_COLUMN);
		channel.mName = c.getString(CONTENT_CHANNEL_NAME_COLUMN);
		channel.mImage = c.getString(CONTENT_CHANNEL_IMAGE_NAME_COLUMN);
		channel.mLastUpdate = c.getLong(CONTENT_CHANNEL_LAST_UPDATE_COLUMN);
		channel.mIsFavorite = c.getInt(CONTENT_CHANNEL_IS_FAVORITE)==1;
		return channel;
	}

}
